package com.jftone.bh.app.directive;

import java.io.Writer;
import java.util.Map;

import com.jftone.bh.app.config.SysConst;
import org.apache.log4j.Logger;

import org.jftone.util.Page;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateModel;

public class PageDirective implements TemplateDirectiveModel {
	
	private Logger logger = Logger.getLogger(PageDirective.class);
			
	private Page page;
	
	public PageDirective(Page page){
		super();
		this.page = page;
	}
	
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body)  {
		try {
			Writer out = env.getOut(); 
            out.write(getPageHtml());  
            if(body != null){
            	 body.render(out);  
            }
            out.flush();
		} catch (Exception e) {
			logger.error("自定义分页指令错误", e);
		}
	}
	
	/**
	 * 分页展示样式
	 */
	private String getPageHtml()
	{
		StringBuilder sbHtml = new StringBuilder();
		sbHtml.append("<div id=\""+ SysConst.PAGE_PART+"\" bind=\"page\" class=\"c_page l_mt\">");
		int curPage = page.getCurrentPage();
		int pageNum = page.getPageCount();
		//分页少的情况
		if(pageNum<=7){
			sbHtml.append(getNumberStyle(1, pageNum));
		}else{
			//上一页
			if(curPage > 1){
				sbHtml.append("<a href=\"javascript:void(0);\" data=\""+(curPage-1)+"\" class=\"prev\"></a>");
			}

			if(curPage<=4){
				sbHtml.append(getNumberStyle(1, 6));
				sbHtml.append("<span class=\"dot\">...</span>");
				sbHtml.append("<a href=\"javascript:void(0);\" data=\""+pageNum+"\">"+pageNum+"</a>");
			}else if(curPage>4 && curPage<pageNum-3){
				sbHtml.append("<a href=\"javascript:void(0);\" data=\"1\">1</a>");
				sbHtml.append("<span class=\"dot\">...</span>");
				sbHtml.append(getNumberStyle(curPage-2, curPage+2));
				sbHtml.append("<span class=\"dot\">...</span>");
				sbHtml.append("<a href=\"javascript:void(0);\" data=\""+pageNum+"\">"+pageNum+"</a>");
			}else if(curPage >=pageNum-3){
				sbHtml.append("<a href=\"javascript:void(0);\" data=\"1\">1</a>");
				sbHtml.append("<span class=\"dot\">...</span>");
				sbHtml.append(getNumberStyle(pageNum-5, pageNum));
			}
			
			//下一页
			if(curPage < pageNum){
				sbHtml.append("<a href=\"javascript:void(0);\" data=\""+(curPage+1)+"\" class=\"next\"></a>");
			}
			
			if(pageNum>7){
				sbHtml.append("<input type=\"text\" id=\""+SysConst.PAGE_NO+"\" value=\""+curPage+"\"/>");
				sbHtml.append("<a id=\""+SysConst.PAGE_BTN+"\" href=\"javascript:void(0)\">跳转</a>");
			}
		}
		sbHtml.append("<div class=\"fr total\">共查询到<i class=\"e_blue\"> "+page.getRecordCount()+" </i>条记录</div></div>");
		return sbHtml.toString();
	}
	
	private String getNumberStyle(int start, int end){
		StringBuilder sb = new StringBuilder();
		for(int i=start; i<=end; i++){
			if(page.getCurrentPage()!=i){
				sb.append("<a href=\"javascript:void(0);\" data=\""+i+"\">"+i+"</a>");
			}else{
				sb.append("<span>"+i+"</span>");
			}
		}
		return sb.toString();	
	}


}
